<template>
  <div class="container">
    <van-nav-bar
      title="编辑资料"
      left-arrow
      left-text="返回"
      placeholder
      @click-left="$router.go(-1)"
    />
    <div class="iconImg">
      <van-image
        round
        fit="cover"
        width="120"
        height="120"
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <!-- 名字修改按钮 -->
      <van-cell
        title="名称"
        is-link
        :value="userInfo.name"
        @click="dialogClick"
      />
      <!-- 性别修改按钮 -->
      <van-cell
        title="性别"
        is-link
        :value="userInfo.gender ? '女' : '男'"
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        is-link
        :value="userInfo.birthday"
        @click="showDate = true"
      />
    </van-cell-group>
    <!-- 名字修改弹出框 -->
    <van-dialog
      v-model="showDialog"
      title="修改昵称"
      show-cancel-button
      @confirm="updateNickname"
    >
      <van-field
        ref="name"
        placeholder="请输入昵称"
        v-model="newName"
      ></van-field>
    </van-dialog>
    <!-- 性别修改弹出狂 -->
    <van-popup
      v-model="showPopup"
      position="bottom"
      round
      :style="{ height: '20%' }"
    >
      <van-nav-bar
        title="修改性别"
        left-arrow
        left-text="取消"
        placeholder
        @click-left="showPopup = false"
      />
      <van-cell title="男" icon="user-o" @click="updateGender(0)" />
      <van-cell title="女" icon="contact" @click="updateGender(1)" />
    </van-popup>
    <!-- 生日修改弹出框 -->
    <van-popup v-model="showDate" position="bottom" round>
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDate = false"
        @confirm="updateBirthday"
      />
    </van-popup>
    <div class="cropper" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        centerBox
        autoCropWidth="120"
        autoCropHeight="120"
      >
      </vueCropper>
      <van-button class="subBtn" @click="submitPhoto" type="primary"
        >确认</van-button
      >
      <van-button class="cancelBtn" @click="showCropper = false" type="info"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { editUserInfo } from "./user.js";
import moment from "moment";
import { editUserIcon } from "./user.js";
import { VueCropper } from "vue-cropper";
export default {
  name: "edit",
  // 注册裁剪组件
  components: {
    VueCropper,
  },
  data() {
    return {
      // 姓名弹窗默认隐藏
      showDialog: false,
      // 姓名用空的字符串存起来
      newName: "",
      // 性别弹窗默认隐藏
      showPopup: false,
      gender: "",
      // 生日弹窗默认隐藏
      showDate: false,
      // 最小时间和最大时间以及现在的默认时间
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2021, 10, 1),
      currentDate: new Date(),
      // 裁剪图片的弹窗默认隐藏
      showCropper: false,
      img: "",
    };
  },
  methods: {
    // 这是昵称修改的代码
    dialogClick() {
      // 点击后弹窗出现
      this.showDialog = true;
      // 得到最新修改的数据
      this.newName = this.userInfo.name;
      // this.$nextTick()相当于一个高级定时器, 在页面渲染后操作里面的事件:输入框聚焦
      this.$nextTick(() => {
        this.$refs.name.focus();
      });
    },
    // 调用修改名字的点击事件
    updateNickname() {
      // 调用user.js中的axios请求方法,此方法需要传参
      editUserInfo({ name: this.newName }).then((res) => {
        console.log(res);
        // this.$toast全局可使用的轻弹窗
        this.$toast.success("修改成功");
        // VueX的使用 this.$store在main.js中已经注册了,可以全局使用   this.$store.commit和mutations相关
        this.$store.commit("setUserInfo", {
          // 展开userInfo,将里面的name用新的name覆盖
          ...this.userInfo,
          // 将页面中的name渲染成传入的name
          name: res.data.data.name,
        });
      });
    },

    // 这是修改性别的代码
    // 点击事件(编辑性别)触发,需要传入参数gender
    updateGender(gender) {
      // console.log(gender);
      editUserInfo({ gender }).then((res) => {
        // console.log({ gender });
        console.log(res);
        this.$toast.success("修改成功");
        this.$store.commit("setUserInfo", {
          ...this.userInfo,
          gender: gender,
        });
        // console.log(this.userInfo.gender);
        this.showPopup = false;
      });
    },

    // 修改生日的代码
    updateBirthday() {
      console.log(this.currentDate);
      let birthday = moment(this.currentDate).format("YYYY-MM-DD");
      console.log(birthday);
      editUserInfo({ birthday }).then((res) => {
        console.log(res);
        this.$toast.success("修改成功");
        this.$store.commit("setUserInfo", {
          ...this.userInfo,
          birthday: res.data.data.birthday,
        });
        this.showDate = false;
      });
    },

    // 修改头像
    afterRead(file) {
      console.log(file);
      this.img = file.content;
      this.showCropper = true;
    },
    submitPhoto() {
      this.$refs.cropper.getCropBlob((blob) => {
        const fd = new FormData();
        fd.append("photo", blob);
        editUserIcon(fd).then((res) => {
          console.log(res);
          this.$toast.success("修改成功");
          this.$store.commit("setUserInfo", {
            ...this.userInfo,
            photo: res.data.data.photo,
          });
          this.showCropper = false;
        });
      });
    },
  },
  // 此处使用mapstate方法对userInfo进行计算
  computed: {
    ...mapState(["userInfo"]),
  },
};
</script>

<style lang="less" scoped>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
::v-deep .van-nav-bar {
  background-color: pink;
  .van-icon {
    color: white;
  }
  .van-nav-bar__text {
    color: white;
  }
  .van-nav-bar__title {
    color: white;
  }
}
.iconImg {
  height: 150px;
  text-align: center;
  margin-top: 25px;
}
.van-popup {
  .van-cell .van-icon-user-o {
    color: #1989fa;
  }
  .van-cell .van-icon-contact {
    color: #ee0a24;
  }
}
::v-deep .van-uploader {
  opacity: 0;
  position: absolute;
  left: 50%;
  width: 120px;
  height: 120px;
  transform: translateX(-50%);
  .van-uploader__upload {
    width: 125px;
    height: 120px;
  }
}
.cropper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  .subBtn {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .cancelBtn {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>